package com.zcf.service.impl;

import com.zcf.pojo.Dining;
import com.zcf.pojo.Notice;
import com.zcf.common.json.Body;
import com.zcf.common.result.PageResult;
import com.zcf.common.result.ResultVo;
import com.zcf.mapper.DiningMapper;
import com.zcf.service.DiningService;
import com.zcf.vo.in.PageVo;
import com.zcf.vo.out.AMapUtils;
import com.zcf.vo.out.LngLat;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class DiningServiceImpl extends ServiceImpl<DiningMapper, Dining> implements DiningService {

	@Autowired
	DiningMapper diningMapper;

	@Override
	public Body addDiningAll() {
		Dining dining = new Dining();
		List<Dining> list = dining.selectAll();
		if (list.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.BODY_500;
	}

	@Override
	public Body getTuijian(String address) {
		List<Dining> lists = diningMapper.getTuijian(address);
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.newInstance("查询失败");
	}

	@Override
	public Body queryCountSou(String diningName, String productName) {

//		List<Dining> list = diningMapper.querydiningName(diningName);
//		if (list.size() > 0) {
//			return Body.newInstance(list);
//		}
		List<Dining> lists = diningMapper.queryCountSou(diningName, productName);
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.newInstance("查询失败！");
	}

	@Override
	public ResultVo queryXianJian(PageVo pv, Integer sum, Double lat1, Double lng1) {
		List<Dining> list = diningMapper.queryXianJian(sum, lat1, lng1);
		List<Dining> lists = new ArrayList<>();
			double distance = 0.0;
		for (Dining dining : list) {
			Notice notice = new Notice();
			EntityWrapper<Notice> ew = new EntityWrapper<>();
			ew.eq("din_id", dining.getId());
			notice = notice.selectOne(ew);
			if (notice != null) {
				dining.setConcent(notice.getContent());
			}
			if (lat1 != null && lng1 != null) {
				LngLat start = new LngLat(lng1, lat1);
				Double lng2 = Double.parseDouble(dining.getLongitude());
				Double lat2 = Double.parseDouble(dining.getLatitude());
				LngLat end = new LngLat(lng2, lat2);
				distance = AMapUtils.calculateLineDistance(start, end);
			}
//			if(distance>=0) {
				dining.setDistance(distance);
				lists.add(dining);
//			}
		}
		if (pv.getPage() != null && pv.getPageSize() != null) {
			PageHelper.startPage(pv.getPage(), pv.getPageSize());
		}
		PageResult pr = PageResult.result(lists);
		ResultVo rv = new ResultVo();
		rv.setList(pr.getRows());
		rv.setTotal(pr.getTotal());
		return rv;
	}

}
